package cc.owoo.godpen.network.http;

import cc.owoo.godpen.analysis.json.Json;
import cc.owoo.godpen.util.Path;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Locale;

/**
 * HTTP请求
 * Created by nimensei
 * 2022-02-24 下午 05:36
 **/
public class Request extends AbstractRequest implements MessageRead {
    boolean autoContentLength = true;// 是否自动计算数据长度

    public Request() {
    }

    public Request(String url) {
        setUrl(url);
    }

    @Override
    public void read(InputStream input) throws IOException {
        HttpAnalysis.read(input, this, this);
    }

    @Override
    public void analysisAgreement(String string) {
        String[] array = string.split(" ");
        if (array.length < 3)
            return;
        setMethod(array[0].toUpperCase(Locale.ROOT));
        setUrl(array[1]);
        setVersion(array[2]);
    }

    @Override
    public String setParam(String key, String value) {
        return super.setParam(key, value);
    }

    @Override
    public String getParam(String key) {
        return super.getParam(key);
    }

    @Override
    public String getUrl() {
        return super.getUrl();
    }

    @Override
    public String getUrl(boolean isAgreement, boolean isHost, boolean isPath, boolean isParams) {
        return super.getUrl(isAgreement, isHost, isPath, isParams);
    }

    @Override
    public int setHeader(String string) {
        return super.setHeader(string);
    }

    @Override
    public boolean setHeader(String key, String value) {
        return super.setHeader(key, value);
    }

    @Override
    public void setAgreement(String agreement) {
        super.setAgreement(agreement);
    }

    @Override
    public boolean setHostString(String host) {
        return super.setHostString(host);
    }

    @Override
    public void setHost(InetAddress host) {
        super.setHost(host);
    }

    @Override
    public void setPort(int port) {
        super.setPort(port);
    }

    @Override
    public void setUrl(String url) {
        super.setUrl(url);
    }

    @Override
    public void setPath(String path) {
        super.setPath(path);
    }

    @Override
    public void setPath(Path path) {
        super.setPath(path);
    }

    @Override
    public Path getPath() {
        return super.getPath();
    }


    @Override
    public void setMethod(String method) {
        super.setMethod(method);
    }

    @Override
    public String getMethod() {
        return super.getMethod();
    }

    @Override
    public boolean isMethod(String method) {
        return super.isMethod(method);
    }

    @Override
    public void setUserAgent(String string) {
        super.setUserAgent(string);
    }

    @Override
    public void setContentType(String string) {
        super.setContentType(string);
    }

    @Override
    public void setCookie(String string) {
        super.setCookie(string);
    }

    @Override
    public void setContent(Json json) {
        super.setContent(json);
    }

    @Override
    public void setContent(byte[] content) {
        super.setContent(content);
    }

    @Override
    public boolean setContent(File file) {
        return super.setContent(file);
    }

    /**
     * 是否开启了自动设置ContentLength，ContentLength用来告诉服务需要接收的数据的长度，默认开启
     *
     * @return 是否开启了自动设置ContentLength
     */
    public boolean isAutoContentLength() {
        return autoContentLength;
    }

    /**
     * 设置是否开启自动设置ContentLength，ContentLength用来告诉服务需要接收的数据的长度，默认开启
     *
     * @param autoContentLength 是否开启自动设置ContentLength
     */
    public void setAutoContentLength(boolean autoContentLength) {
        this.autoContentLength = autoContentLength;
    }
}
